package com.ruoyi.khpx.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.khpx.domain.Cxdw;
import com.ruoyi.khpx.domain.CxdwFj;
import com.ruoyi.khpx.service.ICxdwService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 创新单位Controller
 *
 * @author huacai
 * @date 2024-12-02
 */
@RestController
@RequestMapping("/khpx/cxdw")
public class CxdwController extends BaseController {
    @Resource
    private ICxdwService cxdwService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    /**
     * 查询创新单位列表
     */
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cxdw cxdw) {
        startPage();
        List<Cxdw> list = cxdwService.selectCxdwList(cxdw);
        return getDataTable(list);
    }

    /**
     * 导出创新单位列表
     */
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:export')")
    @Log(title = "创新单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cxdw cxdw) {
        List<Cxdw> list = cxdwService.selectCxdwList(cxdw);
        ExcelUtil<Cxdw> util = new ExcelUtil<Cxdw>(Cxdw.class);
        util.exportExcel(response, list, "创新单位数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Cxdw> util = new ExcelUtil<Cxdw>(Cxdw.class);
        util.importTemplateExcel(response, "创新单位数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "创新单位", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Cxdw> util = new ExcelUtil<Cxdw>(Cxdw.class);
        InputStream inputStream = file.getInputStream();
        List<Cxdw> list = util.importExcel(inputStream);
        inputStream.close();
        int count = cxdwService.batchInsertCxdw(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取创新单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:query')")
    @GetMapping(value = "/{cxdwId}")
    public AjaxResult getInfo(@PathVariable("cxdwId") String cxdwId) {
        return success(cxdwService.selectCxdwByCxdwId(cxdwId));
    }

    /**
     * 新增创新单位
     */
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:add')")
    @Log(title = "创新单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Cxdw cxdw) {
        cxdw.setUserId(Math.toIntExact(getUserId())); //用户ID
        cxdw.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        cxdw.setCreateBy(getUsername()); //创建人
        cxdw.setIdWhere("存在");

        int i = cxdwService.insertCxdw(cxdw);
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(cxdw.getCxdwId());
        infoRunTaskData.setBusinessType(14); //业务模块: 考核评选-创新单位
        infoRunTaskData.setBusinessTitle(cxdw.getSbNd() + cxdw.getSbDw()); //任务名称: 申报年度 + 申报单位
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改创新单位
     */
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:edit')")
    @Log(title = "创新单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Cxdw cxdw) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(cxdw.getSbNd() + cxdw.getSbDw()); //任务名称: 申报年度 + 申报单位
        infoRunTaskData.setBusinessKey(cxdw.getCxdwId());
        infoRunTaskData.setBusinessType(14); //业务模块: 考核评选-创新单位
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(cxdwService.updateCxdw(cxdw));
    }

    /**
     * 删除创新单位
     */
    @PreAuthorize("@ss.hasPermi('khpx:cxdw:remove')")
    @Log(title = "创新单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cxdwIds}")
    public AjaxResult remove(@PathVariable String[] cxdwIds) {
        //同时删除业务关系数据
        for (String cxdwId : cxdwIds) {
            Cxdw cxdw = cxdwService.selectCxdwByCxdwId(cxdwId);
            iInfoRunTaskDataService.deleteData(cxdw.getCxdwId(), 14);//业务模块: 考核评选-创新单位
        }
        return toAjax(cxdwService.deleteCxdwByCxdwIds(cxdwIds));
    }

    /**
     * 获得创新单位主键ID (UUID)
     */
    @GetMapping("/getCxDwId")
    public AjaxResult getCxDwId() {
        String getCxDwId = IdUtils.fastSimpleUUID();
        return success(getCxDwId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertCxdwFjByCxdwId(@RequestBody CxdwFj cxdwFj) {
        return toAjax(cxdwService.insertCxdwFjByCxdwId(cxdwFj));
    }

    /**
     * 根据ID和fileType查询附件列表
     * @param sId 主表ID
     * @param fileType 文件类型
     * @return 附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectCxdwFjList(String sId, String fileType) {
        CxdwFj cxdwFj = new CxdwFj();
        cxdwFj.setCxdwId(sId);
        cxdwFj.setFileType(fileType);
        return AjaxResult.success(cxdwService.selectCxdwFjList(cxdwFj));
    }

    /**
     * 删除创新单位附件
     */
    @Log(title = "创新单位附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteCxdwFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(cxdwService.deleteCxdwFjByFjIds(fjIds));
    }

}
